import {
  getComList,
  getAreaList,
  getDictList,
  getDetailInfo,
  insertParam,
  getMkupPayPlcyList,
  updateMkupPayPlcy,
  deleteMkupPayPlcy,
  saveMkupPayPlcy,
  getAss,
  getListCorpDict,
} from '@/services/paramset';
import { message } from 'antd';

export default {
  state: {
    pageData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    assList: [],
    mkuRecord: {},
    addModVisible: false,
    treeData: [],
    corpDictList: [],
    data: {
      // insUnifyFlag: 1,
      // autoDeclFlag: 1,
      // insAddDeclPlcy: 1,
      // accuAddDeclPlcy: 1,
      newIOPDDList: [],
      newAOPDDList: [],
    },
    lastvalue: '',
    dictData: {
      ADD_BILL_PLCY: [
        // {
        //   itemName: '增员数据当月账单统计',
        //   itemCode: '1',
        //   showOrder: 1,
        // },
        // {
        //   itemName: '增员数据次月账单统计',
        //   itemCode: '2',
        //   showOrder: 2,
        // },
      ],
      DECL_PLCY: [
        // {
        //   itemName: '当月申报进当月账单',
        //   itemCode: '1',
        //   showOrder: 1,
        // },
        // {
        //   itemName: '当月申报进下月账单',
        //   itemCode: '2',
        //   showOrder: 2,
        // },
      ],
      BILL_GEN_PLCY: [
        // {
        //   itemName: '生成当月账单',
        //   itemCode: '1',
        //   showOrder: 1,
        // },
        // {
        //   itemName: '生成下月账单',
        //   itemCode: '2',
        //   showOrder: 2,
        // },
      ],
      OPER_SET: [
        // {
        //   itemName: '单流',
        //   itemCode: '1',
        //   showOrder: 1,
        // },
        // {
        //   itemName: '双流',
        //   itemCode: '2',
        //   showOrder: 2,
        // },
      ],
      CLCTNPLCY: [
        // {
        //   itemName: '当月申报当月',
        //   itemCode: '1',
        // },
        // {
        //   itemName: '当月申报下月',
        //   itemCode: '2',
        // },
      ],
      YES_NO_FLAG: [
        // {
        //   itemName: '是',
        //   itemCode: '1',
        // },
        // {
        //   itemName: '否',
        //   itemCode: '2',
        // },
      ],
    },
  },
  effects: {
    // 获取字典
    *fetchDictList({ payload }, { call, put }) {
      const response = yield call(getDictList, payload);
      if (response.success) {
        yield put({
          type: 'getDictData',
          payload: response.data,
        });
      } else {
        message.error(response.message);
      }
    },
    *fetchAss({ payload }, { call, put }) {
      const response = yield call(getAss, payload);
      if (response.success) {
        yield put({
          type: 'getAss',
          payload: response.data,
        });
      } else {
        message.error(response.message);
      }
    },
    *fetchComList({ payload }, { call, put }) {
      const response = yield call(getComList, payload);
      if (response.success) {
        yield put({
          type: 'getTreeData',
          payload: response.data,
        });
        return response.data;
      }
      return message.error(response.message);
    },
    *fetchAreaList({ payload }, { call, put }) {
      const response = yield call(getAreaList, { corpId: payload.params.corpId });
      const {
        isLeaf,
        params: { corpId, corpName },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getChildrenData',
          payload: { data: response.data, isLeaf, corpId, corpName },
        });
        yield put({
          type: 'updateModelsData',
          payload: { corpId },
        });
      } else {
        message.error(response.message);
      }
    },
    *fetchInfo({ payload }, { call, put }) {
      const response = yield call(getDetailInfo, payload);
      if (response.success) {
        yield put({
          type: 'handleData',
          payload: response.data,
        });
      } else {
        message.error(response.message);
      }
    },

    *saveParam({ payload, callback }, { call }) {
      const response = yield call(insertParam, payload);
      if (response.success) {
        // yield put({
        //   type: 'fetchInfo',
        //   payload: {
        //     areaId: payload.areaId,
        //     corpId: payload.corpId,
        //   },
        // });
        callback(payload);
        message.success('保存成功!');
      } else {
        message.error(response.message);
      }
    },
    *fetchMkupPayPlcy({ payload }, { call, put }) {
      const response = yield call(getMkupPayPlcyList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getMkupPayPlcyList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    *fetchDeleteMkupPayPlcy({ payload }, { call }) {
      const response = yield call(deleteMkupPayPlcy, payload);
      if (response.success) {
        message.success('删除成功!');
        return response;
      }
      return message.warning(response.message || '操作失败：服务端错误');
    },

    *fetchSaveMkupPayPlcy({ payload }, { call, select, put }) {
      const { operMkupPayPlcyId, vno } = yield select(state => state.paramset.mkuRecord);
      const response = operMkupPayPlcyId
        ? yield call(updateMkupPayPlcy, { ...payload, operMkupPayPlcyId, vno })
        : yield call(saveMkupPayPlcy, payload);
      if (response.success) {
        message.success('保存成功!');
        yield put({
          type: 'updateModelsData',
          payload: {
            addModVisible: false,
          },
        });
        const {
          pageData: { pagination },
          corpId,
        } = yield select(state => state.paramset);
        const values = {
          pageArg: { currentPage: pagination.current, pageSize: pagination.pageSize },
          operMkupPayPlcy: { corpId, areaId: payload.areaId },
        };
        yield put({
          type: 'fetchMkupPayPlcy',
          payload: values,
        });
      } else {
        message.warning(response.message || '提交失败：服务端错误');
      }
    },
    *fetchListCorpDict({ payload }, { call, put }) {
      const response = yield call(getListCorpDict, payload);
      yield put({
        type: 'updateModelsData',
        payload: { corpDictList: [] },
      });
      if (response.success) {
        yield put({
          type: 'getListCorpDict',
          payload: response.data,
        });
      } else {
        message.warning(response.message);
      }
    },
  },

  reducers: {
    getAss(state, { payload }) {
      return {
        ...state,
        assList: payload,
      };
    },
    getListCorpDict(state, { payload }) {
      const unLimit = { itemCode: 'null', itemName: '不限' };
      return {
        ...state,
        corpDictList: [unLimit, ...payload],
      };
    },
    getMkupPayPlcyList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      // 可补缴月数 mkupPayMthRuleName mkupPayMth
      // 补缴基数 mkupPayBaseRuleName(mkupPayBaseUprLmt~mkupPayBaseLwrLmt)
      // 补缴比例 mkupPayPropRuleName(mkupPayCorpProp+mkupPayEmpProp)
      data.pageRecords.map(item => {
        const newItem = item;
        newItem.mkupPayMths =
          item.mkupPayMthRule !== 3
            ? (item.mkupPayMth && item.mkupPayMthRuleName.concat(item.mkupPayMth)) ||
              item.mkupPayMthRuleName
            : item.mkupPayMthRuleName;
        newItem.mkupPayBase = item.mkupPayBaseRuleName;
        //   +item.mkupPayBaseRule === 2
        //     ? item.mkupPayBaseRuleName
        //         .concat(item.mkupPayBaseUprLmt)
        //         .concat('~')
        //         .concat(item.mkupPayBaseLwrLmt)
        //     : item.mkupPayBaseRuleName;
        newItem.mkupPayProp = +item.mkupPayCorpProp
          ? item.mkupPayPropRuleName
              .concat(item.mkupPayCorpProp)
              .concat('+')
              .concat(item.mkupPayEmpProp)
          : item.mkupPayPropRuleName;
        newItem.mkupPayMthLwr = item.mkupPayMthUprLmt
          ? `${item.mkupPayMthLwrLmt}`.concat('≤补缴月<').concat(item.mkupPayMthUprLmt)
          : `${item.mkupPayMthLwrLmt}`.concat('≤补缴月');
        return true;
      });

      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    updateModelsData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 获取字典数据
    getDictData(state, { payload }) {
      return {
        ...state,
        dictData: payload,
      };
      // const treeData = payload.map(item => ({
      //   ...item,
      //   title: item.companyName,
      //   key: item.corpId,
      // }));
      // // const treeData = [];
      // return {
      //   ...state,
      //   treeData,
      // };
    },
    // 获取树形数据
    getTreeData(state, { payload }) {
      const treeData = payload.map(item => ({
        ...item,
        title: item.companyName,
        key: item.corpId,
      }));
      // const treeData = [];
      return {
        ...state,
        treeData,
      };
    },
    getChildrenData(state, { payload }) {
      const childrenData = payload.data.map(item => ({
        ...item,
        title: item.areaName,
        key: `${item.areaId}+${item.corpId}`,
        dataInfo: item,
        corpName: payload.corpName,
        isLeaf: payload.isLeaf || Number(item.functionType) === 1,
      }));
      const treeData = state.treeData.map(item => ({
        ...item,
        children: item.key === payload.corpId ? childrenData : item.children,
      }));
      return {
        ...state,
        treeData,
      };
    },
    handleData(state, { payload }) {
      return {
        ...state,
        data: payload,
      };
    },
    autoChange(state, { payload }) {
      const { autoDeclFlag } = payload;
      return {
        ...state,
        data: { ...state.data, autoDeclFlag },
      };
    },
    // yield put({
    //   type: 'getDictData',
    //   payload: response.data,
    // });
  },
};
